Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

deWorld_Helper.hpp

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////////
00002 /// @file deWorld_Helper.hpp
00003 ///
00004 /// @brief Destiny "World" storage, helper file
00005 ///
00006 /// @author Assassin
00007 ///
00008 /// This file is the intellectual property of Novus Delta, LLC.. Usage of the
00009 /// contents of this file is subject to the Destiny3D Member License which
00010 /// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
00011 ///
00012 /// This file is distributed "AS IS" without warranty of any kind.  Novus
00013 /// Delta, LLC. does not guarantee the fitness of the contents of this file
00014 /// for any particular purpose.
00015 ///
00016 /// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
00017 ///
00018 /// <hr>
00019 ///                                 Change History
00020 /// <hr>
00021 ///
00022 /// @date July 2003
00023 /// @author Assassin
00024 /// @remarks Creation
00025 ///
00026 ///////////////////////////////////////////////////////////////////////////////
00027 
00028 #ifndef DEWORLD_HELPER_HPP
00029 #define DEWORLD_HELPER_HPP
00030 
00031 #include "deWorld.hpp"
00032 #include "deFile.hpp"
00033 
00034 // this is meant to be an inline-class that has "local" methods inside the
00035 // same DLL as the class that inherits it
00036 //template <class T>
00037 /// inherit from this class instead of IdeWorldObject to get basic functionality implemented
00038 class deWorldObject : virtual public IdeWorldObject, public deRefCountBase
00039 {
00040 private:
00041     deWorldID   m_WorldID;
00042     IdeWorld*   m_pWorld;
00043     deObjectID  m_ObjectID;
00044     void*       m_PrivateData;
00045     long        m_PrivDataSize;
00046 protected:
00047     virtual void OnWorldIDSet() {} // override this if you need to
00048     deWorldObject()
00049         :   m_WorldID(0), m_ObjectID(0),
00050             m_pWorld(NULL), m_PrivateData(NULL), m_PrivDataSize(0)
00051     { }
00052     deWorldObject(deWorldID WorldID, deObjectID ObjectID)
00053         : m_WorldID(WorldID), m_ObjectID(ObjectID), m_PrivateData(NULL), m_PrivDataSize(0)
00054     { m_pWorld = IdeWorld_GetManager()->GetWorld(m_WorldID); }
00055     virtual ~deWorldObject()
00056     {
00057         if (m_pWorld)
00058             m_pWorld->RemoveWorldObject(m_ObjectID);
00059         if (m_PrivateData)
00060             free(m_PrivateData);
00061     }
00062     deObjectID GetObjectID() 
00063     { return m_ObjectID; }
00064     deWorldID GetWorldID() 
00065     { return m_WorldID; }
00066     deBoolean SetWorldID(deWorldID WorldID, deObjectID ObjectID = 0)
00067     {
00068         if (WorldID == m_WorldID)
00069             return deTRUE;
00070         deObjectID oldID = m_ObjectID;
00071         IdeWorld * newWorld = m_pWorld;
00072         m_WorldID = 0;
00073         m_ObjectID = 0;
00074         m_pWorld = NULL;
00075         if (newWorld)
00076             newWorld->RemoveWorldObject(oldID);
00077         newWorld = IdeWorld_GetManager()->GetWorld(WorldID); 
00078         if (!newWorld)
00079             return deTRUE;
00080         m_pWorld = newWorld;
00081         if (ObjectID != 0)
00082         {
00083             m_ObjectID = ObjectID;
00084             if (!m_pWorld->AddWorldObjectWithID(((IdeWorldObject*)this)))
00085                 return deFALSE;
00086         }
00087         else
00088         {
00089             m_ObjectID = m_pWorld->AddWorldObject(((IdeWorldObject*)this));
00090             if (m_ObjectID == 0)
00091                 return deFALSE;
00092         }
00093         m_WorldID = WorldID;
00094         OnWorldIDSet();
00095         return deTRUE;
00096     }
00097     deBoolean SetPrivateData(void* pData, long Size)
00098     {
00099         if (!pData)
00100         {
00101             if (m_PrivateData)
00102                 free(m_PrivateData);
00103             m_PrivateData = NULL;
00104             m_PrivDataSize = 0;
00105         }
00106         if (Size <= 0)
00107             return deFALSE;
00108         m_PrivateData = malloc(Size);
00109         m_PrivDataSize = Size;
00110         memcpy(m_PrivateData, pData, m_PrivDataSize);
00111         return deTRUE;
00112     }
00113     void* GetPrivateData(long & pSize)
00114     {
00115         pSize = m_PrivDataSize;
00116         return m_PrivateData;
00117     }
00118 private:
00119     deBoolean DeSerializeLoad()
00120     { return deTRUE; }
00121 };
00122 
00123 #define WorldObjectClassDef \
00124 private: \
00125     static const char* s_WOTypeName; \
00126 public: \
00127     static IdeWorldObject* CreateWorldObject(void); \
00128     const char* GetWOTypeName(void); \
00129     deBoolean Serialize(IdeFile * File); \
00130     deBoolean DeSerialize(IdeFile * File, long DataLength)
00131 #define WorldObjectClassImp(classname) \
00132     const char* classname::GetWOTypeName(void) { return s_WOTypeName; } \
00133     IdeWorldObject* classname::CreateWorldObject(void) { return (IdeWorldObject*)(new classname); } \
00134     const char* classname::s_WOTypeName = #classname
00135 #define WorldObjectRegister(classname) \
00136     (IdeWorld_GetManager()->RegisterWOClass(#classname, classname::CreateWorldObject))
00137 
00138 
00139 // some utility functions for making error-checked file reading & writing easier
00140 template <int N, typename Y>
00141 static deBoolean De3d_Read_CheckLength(IdeFile* FS, Y& Target, u32 &Length, u32 MaxLength)
00142 {
00143     //T tempvar;
00144     char tempvar[N];
00145     if (Length + N > MaxLength)
00146         return deFALSE;
00147     if (N != FS->Read(&tempvar, N))
00148         return deFALSE;
00149     Length += N;
00150     //Target = (Y)tempvar;
00151     memcpy(&Target, tempvar, N);
00152     return deTRUE;
00153 }
00154 
00155 static deBoolean De3d_Read_CheckLength(IdeFile* FS, void* Target, u32 &Length, u32 MaxLength, u32 ByteSize)
00156 {
00157     if (Length + ByteSize > MaxLength)
00158         return deFALSE;
00159     if (ByteSize != (u32)FS->Read(Target, ByteSize))
00160         return deFALSE;
00161     Length += ByteSize;
00162     return deTRUE;
00163 }
00164 
00165 template <typename T>
00166 static deBoolean De3d_Write_CheckLength(IdeFile* FS, T Source)
00167 {
00168     if (sizeof(T) != FS->Write((void*)(&Source), sizeof(T)))
00169         return deFALSE;
00170     return deTRUE;
00171 }
00172 
00173 static deBoolean De3d_Write_CheckLength(IdeFile* FS, void* Source, u32 ByteSize)
00174 {
00175     if (ByteSize != (u32)FS->Write(Source, ByteSize))
00176         return deFALSE;
00177     return deTRUE;
00178 }
00179 
00180 #define DE3D_STREAM_PUT(FS,var,vartype) \
00181 if(!De3d_Write_CheckLength<vartype>(FS,(vartype)var)) return deFALSE
00182 #define DE3D_STREAM_PUTP(FS,var,size) \
00183 if(!De3d_Write_CheckLength(FS,var,size)) return deFALSE
00184 
00185 #define DE3D_STREAM_GET(FS,var,vartype,length,maxlength) \
00186 if(!De3d_Read_CheckLength<sizeof(vartype)>(FS,var,length,maxlength)) return deFALSE
00187 #define DE3D_STREAM_GETP(FS,var,size,length,maxlength) \
00188 if(!De3d_Read_CheckLength(FS,var,length,maxlength, size)) return deFALSE
00189 
00190 #endif // DEWORLD_HELPER_HPP

Generated on Mon Sep 12 19:58:41 2005 for Destiny3D by doxygen1.3-rc3